package lt.liepasoft.locr.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class UrlInputDialog extends JDialog {

	private final static int 
		WIDTH=280, 
		HEIGHT=120;
	
	private JPanel mainPanel;
	private JTextField urlField;
	private JButton ok, cancel;
	private String result = null;
	
	
	private UrlInputDialog(){
		super(OcrMainFrame.getInstance(), "Enter Image URL", true);
		mainPanel = new JPanel(new BorderLayout());
		
		JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		topPanel.add(new JLabel("Url:"));
		topPanel.setBorder(BorderFactory.createEmptyBorder(0, 10 , 0, 0));
		mainPanel.add(topPanel, BorderLayout.NORTH);
		
		JPanel centerPanel = new JPanel();
		urlField = new JTextField(30);
		
		centerPanel.add(urlField, BorderLayout.CENTER);
		centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0 , 0, 0));
		
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		
		JPanel bottomPanel = new JPanel(new FlowLayout());
		ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				result = urlField.getText();				
				if (result.trim().isEmpty())
					result = null;		
				dispose();
			}
		});
		bottomPanel.add(ok);
		
		cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				result = null;		
				dispose();
			}
		});
		bottomPanel.add(cancel);
		
		mainPanel.add(bottomPanel, BorderLayout.SOUTH);
		
		setContentPane(mainPanel);
		
		//setSize(250,100);
		Rectangle r = OcrMainFrame.getInstance().getBounds();
		setBounds((int)r.getCenterX()-WIDTH/2, (int)r.getCenterY()-HEIGHT/2, WIDTH, HEIGHT);
        setResizable(false);
        
        setVisible(true);
		//setPreferredSize(new Dimension(200, 100));
	}
	
	public static String showDialog(){
		UrlInputDialog dialog = new UrlInputDialog();
		System.out.println("Dialog Closed");
		return dialog.result;
	}		
}
